package org.vergil.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/")
public class IndexController {

    @Value("${redis.service}")
    private String redisService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @RequestMapping
    @ResponseBody
    public ModelAndView index() {
        return new ModelAndView("index");
    }


    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(String name, String pwd) {
        Map<String, Object> map = new HashMap<>(Map.of(name, pwd));
        redisTemplate.opsForValue().set("testspring","testspring");
        return map;
    }

    @RequestMapping("/redis")
    public Map<String, Object> redis(String message) {
        Map<String, Object> map = new HashMap<>();
        String channel = "chatroom";
        redisTemplate.convertAndSend(channel, "from testData");
        return map;
    }


}
